//
//  iPToEAppDelegate.m
//  iPToE
//
//  Created by Chris Perra on 4/24/08.
//  Copyright Apple Inc 2008. All rights reserved.
//

#import "iPToEAppDelegate.h"
#import "PeriodicElements.h"
#import "AtomicElement.h"
#import "ElementsSortedByNameDataSource.h"
#import "ElementsSortedByAtomicNumberDataSource.h"
#import "ElementsSortedBySymbolDataSource.h"
#import "ElementsSortedByStateDataSource.h"
#import "ElementsTableViewController.h"

@implementation iPToEAppDelegate

@synthesize tabBarController;
@synthesize portraitWindow;

- init {
	if (self = [super init]) {
		portraitWindow = nil;
		tabBarController = nil;
	}
	return self;
}

- (UINavigationController *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass {
	id<ElementsDataSource,UITableViewDataSource> dataSource = [[datasourceClass alloc] init];
	
	ElementsTableViewController *theViewController;
	theViewController = [[ElementsTableViewController alloc] initWithDataSource:dataSource];
	
	UINavigationController *theNavigationController;
	theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
	
	[dataSource release];
	
	[theViewController release];
	
	return theNavigationController;
}

- (void)setupPortraitUserInterface {
	UINavigationController *localNavigationController;
	
	UIWindow *localPortraitWindow;
	localPortraitWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	self.portraitWindow = localPortraitWindow;
	
	[localPortraitWindow release];
	
	
	[portraitWindow setBackgroundColor:[UIColor blackColor]];
	
	tabBarController = [[UITabBarController alloc] init];
	NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
	
	localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[ElementsSortedByNameDataSource class]];
	[localViewControllersArray addObject:localNavigationController];
	
	[localNavigationController release];
	
	localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[ElementsSortedByAtomicNumberDataSource class]];
	[localViewControllersArray addObject:localNavigationController];
	
	[localNavigationController release];
	
	localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[ElementsSortedBySymbolDataSource class]];
	[localViewControllersArray addObject:localNavigationController];
	
	[localNavigationController release];
	
	localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[ElementsSortedByStateDataSource class]];
	[localViewControllersArray addObject:localNavigationController];
	
	[localNavigationController release];
	
	
	tabBarController.viewControllers = localViewControllersArray;
	
	[localViewControllersArray release];
	
	[portraitWindow addSubview:tabBarController.view];
	
	[portraitWindow makeKeyAndVisible];
	
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	[self setupPortraitUserInterface];
	
}

- (void)dealloc {
	[tabBarController release];
	[portraitWindow release];
	[super dealloc];
}

@end